﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kiiro.Pop3Client
{
    public static class Utility
    {
        /// <summary>
        /// Formats text/plain body when Content-Transfer-Encoding=quoted-printable
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static string FormatPlainText(string plainText, string contentTransferEncoding)
        {
            // 7bit
            // quoted-printable
            
            if (contentTransferEncoding == "quoted-printable")
            {
                StringBuilder formattedText = new StringBuilder("");

                while (plainText.IndexOf('=') >= 0)
                {
                    string part = plainText.Substring(0, plainText.IndexOf('='));
                    formattedText.Append(part);

                    plainText = plainText.Substring(plainText.IndexOf('='));
                    if (plainText.Substring(0, 3) == "=3D")
                    {
                        formattedText.Append("=");
                    }
                    plainText = plainText.Substring(3);
                }
                formattedText.Append(plainText);

                return formattedText.ToString();
            }
            else
            {
                return plainText;
            }
        }
    }
}
